package com.fengba.nanzhi.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.login.Login;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.security.spec.ECField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by duan on 2018/4/12.
 * 封装工具类
 */

public class OKManager {
    private static final String BASE_URL = "http://www.nan-zhi.com/app";//请求接口根地址
    //private static final String FX_URL = "http://www.nan-zhi.com/app";//请求接口根地址
    public static final String FX_URL1 = "http://www.nan-zhi.com/app";//请求接口根地址
    private static final String FX_URL = "http://www.nan-zhi.com/app";//请求接口根地址
    private static final String WZ_URL = "http://www.nan-zhi.com/app";//请求接口根地址
    private OkHttpClient client;
    private volatile static OKManager manager;
    private final String  TAG = OKManager.class.getSimpleName();
    private Handler handler;
    //提交json数据
    private static final MediaType JSON = MediaType.parse("application/x-www-form-urlencoded");//mdiatype 这个需要和服务端保持一致
    //提交字符串
    private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown");
    //SESSION
    public String sessionlogin = "";
    private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

    private OKManager(){
        client = new OkHttpClient();
        handler = new Handler(Looper.getMainLooper());
    }

    public static OKManager getInstance(){
        OKManager instance = null;
        if(manager == null){
            synchronized (OKManager.class){
                if(instance == null){
                    instance = new OKManager();
                    manager = instance;
                }
            }
        }
        return manager;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 同步请求，在android开发中不常用，因为会阻塞UI线程
     * @param url
     * @return
     */
    public String syncGetByURL(String url){
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();//同步请求数据
            if (response.isSuccessful()){
                return response.body().string();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 异步请求url返回字符串
     * @param url
     * @param callBack
     */
    public void asyncJsonStringByURL(String url,final Func1 callBack){
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response != null && response.isSuccessful()){
                    onSuccessJosnStringMethod(response.body().string(),callBack);
                }
            }
        });
    }

    /**
     * 设置请求头
     * @param headersParams
     * @return
     */
    private Headers SetHeaders(Map<String, String> headersParams){
        Headers headers=null;
        okhttp3.Headers.Builder headersbuilder=new okhttp3.Headers.Builder();

        if(headersParams != null)
        {
            Iterator<String> iterator = headersParams.keySet().iterator();
            String key = "";
            while (iterator.hasNext()) {
                key = iterator.next().toString();
                headersbuilder.add(key, headersParams.get(key));
                Log.d("get http", "get_headers==="+key+"===="+headersParams.get(key));
            }
        }
        headers=headersbuilder.build();

        return headers;
    }

    /**
     * 异步请求url返回对象
     * @param url
     * @param paramsMap
     * @param callBack
     */
    public void asyncJsonObjectByURL(String url,HashMap<String, String> paramsMap,final func4 callBack){

        //Log.e(sessionlogin,sessionlogin);
        StringBuilder tempParams = new StringBuilder();
        int pos = 0;
        for (String key : paramsMap.keySet()) {
            if (pos > 0) {
                tempParams.append("&");
            }
            tempParams.append(String.format("%s=%s", key, paramsMap.get(key)));
            pos++;
        }
        String params = tempParams.toString();
        //Log.i(TAG,params);
        RequestBody body = RequestBody.create(JSON, params);
        //System.out.println(body);
        //System.out.println(url);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    @Override
                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                        for(Cookie cookie:list){
                            if(cookie.name().equals("nzpay.sesssion")){
                                sessionlogin = cookie.value();
                                cookieStore.put(httpUrl.host(), list);
                            }
                            //Log.e("cookie Name:",cookie.name());
                            //Log.e("cookie Value:",cookie.value());
                        }
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                        List<Cookie> cookies = cookieStore.get(httpUrl.host());
                        return cookies != null ? cookies : new ArrayList<Cookie>();

                    }
                }).build();
        final Request request = new Request
                .Builder()
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        //client.newCall(request).enqueue(new Callback() {
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                onSuccessJsonObjectMethod(e.getMessage(),callBack);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response != null && response.isSuccessful()){
                    onSuccessJsonObjectMethod(response.body().string(),callBack);
                }
            }
        });

    }

    /**
     * 异步请求url返回的是byte数组
     * @param url
     * @param callBack
     */
    public void asyncGetByteByURL(String url,final func2 callBack){
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response != null && response.isSuccessful()){
                    onSuccessByteMethod(response.body().bytes(),callBack);
                }
            }
        });
    }

    /**请求返回的结果是bitmap
     * @param url
     * @param callback
     */
    public Bitmap asyncBitmapByUrl(final String url, final func3 callback){
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("",">>>::asyncBitmapByUrl onFailure!");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response!=null&&response.isSuccessful()) {
                    byte [] data=response.body().bytes();
                    Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length);
                    onSuccessBitmapMethod(bitmap,callback);
                }

            }
        });
        return null;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 模拟表单提交数据
     * @param url
     * @param params
     * @param callBack
     */
    public void sendComplexForm(String url, HashMap<String, String> params,final func4 callBack){
        FormBody.Builder form_builder = new FormBody.Builder();//表单对象
        if(params != null && !params.isEmpty()){
            for (Map.Entry<String,String> entry:params.entrySet()){
                form_builder.add(entry.getKey(),entry.getValue());

            }
        }
        RequestBody request_body = form_builder.build();
        final Request request = new Request.Builder().url(url).post(request_body).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response!=null && response.isSuccessful()){
                    onSuccessJsonObjectMethod(response.body().toString(),callBack);
                }
            }
        });
    }

    /**
     * 异步请求url返回对象get
     * @param url
     * @param paramsMap
     * @param callBack
     */
    public void asyncJsonObjectByURLFromGET(String url,HashMap<String, String> paramsMap,final func4 callBack){

        StringBuilder tempParams = new StringBuilder();
        int pos = 0;
        for (String key : paramsMap.keySet()) {
            if (pos > 0) {
                tempParams.append("&");
            }
            tempParams.append(String.format("%s=%s", key, paramsMap.get(key)));
            pos++;
        }
        String params = tempParams.toString();
        //Log.i(TAG,params);
        RequestBody body = RequestBody.create(JSON, params);
        //System.out.println(body);
        //System.out.println(url);
        final Request request = new Request.Builder().url(url).get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                onSuccessJsonObjectMethod(e.getMessage(),callBack);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response != null && response.isSuccessful()){
                    onSuccessJsonObjectMethod(response.body().string(),callBack);
                }
            }
        });

    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 请求返回的结果是json字符串
     * @param jsonValue
     * @param callBack
     */
    private void onSuccessJosnStringMethod(final String jsonValue, final Func1 callBack){
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null){
                    try {
                        callBack.onResponse(jsonValue);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    /**
     * 返回的结果是数组
     * @param result
     * @param callBack
     */
    private void onSuccessByteMethod(final byte[] result,final func2 callBack){
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null){
                    try {
                        callBack.onResponse(result);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    /**返回的结果是Bitmap
     * @param bitmap
     * @param callBack
     */
    private void onSuccessBitmapMethod(final Bitmap bitmap,final func3 callBack){

        handler.post(new Runnable() {
            @Override
            public void run() {

                if(callBack!=null){
                    try{

                        callBack.onResponse(bitmap);
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally{

                    }

                }
            }
        });
    }

    /**
     * 返回的结果是json对象
     * @param jsonVaule
     * @param callBack
     */
    private void onSuccessJsonObjectMethod(final String jsonVaule,final func4 callBack){
        handler.post(new Runnable() {
            @Override
            public void run() {
            if (callBack != null){
                try {
                    callBack.onResponse(new JSONObject(jsonVaule));
                }catch (Exception e){
                    e.printStackTrace();
                    callBack.onFailure(e.getMessage());
                }
            }
            }
        });
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    interface Func1{
        void onResponse(String result);
    }

    interface func2{
        void onResponse(byte[] result);
    }

    public interface func3{
        void onResponse(Bitmap bitmap);
    }

    public interface func4{
        void onResponse(JSONObject jsonObject);
        void onFailure(String string);
    }


    ///////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 用户登录
     * @param account
     * @param password
     * @param callBack
     */
    public void type0001(String account,String password,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("password", password);
        String url = String.format("%s/%s", BASE_URL, "api/user/land");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 抽疯列表
     * @param callBack
     */
    public void type0002(final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        String url = String.format("%s/%s", BASE_URL, "api/crazyAll/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 获取默认地址
     * @param callBack
     */
    public void type0003(String account, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", account);
        String url = String.format("%s/%s", BASE_URL, "api/address/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 用户登录2
     * @param callBack
     */
    public void type0004(String user_id,String account, String passwrod,String registrationId,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("account", account);
        map.put("password", passwrod);
        map.put("registrationId", registrationId);
        String url = String.format("%s/%s", BASE_URL, "api/user/login");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 生成订单
     * @param callBack
     */
    public void type0005(String user_id,
                         String address_id,
                         String order_money,
                         String goods_id,
                         String buy_number,
                         String remarks,
                         String activity_number,
                         String type,
                         String credit,
                         String crazyrob_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", user_id);
        map.put("address_id", address_id);
        map.put("order_money", order_money);
        map.put("goods_id", goods_id);
        map.put("buy_number", buy_number);
        map.put("remarks", remarks);
        map.put("activity_number", activity_number);
        map.put("type", type);
        map.put("credit", credit);
        map.put("crazyrob_id", crazyrob_id);
        String url = String.format("%s/%s", BASE_URL, "api/Myorder/Submit");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 支付宝支付
     * @param callBack
     */
    public void type0006(String user_id,
                         String card_number,
                         String txnAmt,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", user_id);
        map.put("card_number", card_number);
        map.put("txnAmt", txnAmt);
        String url = String.format("%s/%s", BASE_URL, "api/pay/alipay/create_trade");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 修改订单状态
     * @param user_id
     * @param order_id
     * @param type
     * @param out_trade_no
     * @param callBack
     */
    public void type0007(String user_id,
                         String order_id,
                         String type,
                         String out_trade_no,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", user_id);
        map.put("order_id", order_id);
        map.put("type", type);
        map.put("out_trade_no", out_trade_no);
        String url = String.format("%s/%s", BASE_URL, "api/MyPaymentPassword");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 兑换列表
     * @param callBack
     */
    public void type0008(String pageNum,String pageSize,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        String url = String.format("%s/%s", BASE_URL, "api/exchange/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    //////////////////////////////////范想

    /**
     * 消息
     * @param account
     * @param pageNum
     * @param pageSize
     * @param callBack
     */
    public void type2001(String account,String pageNum,String pageSize,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        String url = String.format("%s/%s", FX_URL, "api/user/messagelist");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 遍历订单
     * @param account
     * @param type
     * @param status
     * @param callBack
     */
    public void type2002(String account,String type,String status,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("type", type);
        map.put("status", status);
        String url = String.format("%s/%s", FX_URL, "api/Myorder/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 首页banner
     * @param callBack
     */
    public void type2003(final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        String url = String.format("%s/%s", FX_URL, "api/firstPage/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 判断是否签到
     * @param callBack
     */
    public void type2004(String id, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/user-sign/list");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 我的疯豆
     * @param callBack
     */
    public void type2005(String id, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/Mycredits");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 签到
     * @param callBack
     */
    public void type2006(String id, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/user-sign/sign");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 向推荐好友索取
     * @param callBack
     */
    public void type2007(String id, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/credits/Claim/Recommend");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 索取数量
     * @param callBack
     */
    public void type2008(String giveid,String myid,String credits, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("giveid", giveid);
        map.put("myid", myid);
        map.put("credits", credits);
        String url = String.format("%s/%s", FX_URL, "api/credits/in");
        asyncJsonObjectByURL(url,map,callBack);
    }



    /**
     * 索取
     * @param callBack
     */
    public void type2009(String id,String account, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("account", account);
        String url = String.format("%s/%s", FX_URL, "api/credits/Claim/Give");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 索取记录 pageNum分页页数 pageSize分页行数
     * @param callBack
     */
    public void type2010(String account,String pageNum,String pageSize, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        String url = String.format("%s/%s", FX_URL, "api/MyclaimLog/list");
        asyncJsonObjectByURL(url,map,callBack);
    }
    /**
     * 赠送搜索
     * @param callBack
     */
    public void type2011(String giveid, String myid, String credits, String id, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("giveid", giveid);
        map.put("myid", myid);
        map.put("credits", credits);
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/credits/Confirm");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     *
     * 疯豆明细
     * @param callBack
     */
    public void type2012(String account, String pageNum, String pageSize, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        String url = String.format("%s/%s", FX_URL, "api/credits/Detaile");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 确认在此赠送
     * @param callBack
     */
    public void type2013(String giveid, String myid, String credits, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("giveid", giveid);
        map.put("myid", myid);
        map.put("credits", credits);
        String url = String.format("%s/%s", FX_URL, "api/credits/ConfirmTo");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 忘记密码
     * @param callBack
     */
    public void type2014(String account, String password, String msg_id,String code, final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("password", password);
        map.put("msg_id", msg_id);
        map.put("code", code);
        String url = String.format("%s/%s", FX_URL, "api/user/forget");
        asyncJsonObjectByURL(url,map,callBack);
    }
    /**
     * 发送验证码
     * @param callBack
     */
    public void type2015(String type, String mobile,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("type", type);
        map.put("mobile", mobile);
        String url = String.format("%s/%s", FX_URL, "api/user/information");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 注册
     * @param callBack
     */
    public void type2016(String account, String password,String parent_invite_code,String msg_id,String code,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("password", password);
        map.put("parent_invite_code", parent_invite_code);
        map.put("msg_id", msg_id);
        map.put("code", code);
        String url = String.format("%s/%s", FX_URL, "api/user/save");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 删除赠送疯豆的订单
     * @param callBack
     */
    public void type2017(String id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/claimLog/del");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 单独遍历订单
     * @param callBack
     */
    public void type2018(String id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", FX_URL, "api/Myorder");
        asyncJsonObjectByURL(url,map,callBack);
    }


    /**
     * 上传微信二维码
     */
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    public void type2019(String id,File file) {
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    @Override
                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                        for(Cookie cookie:list){
                            if(cookie.name().equals("nzpay.sesssion")){
                                cookieStore.put(httpUrl.host(), list);
                            }
                            //System.out.println("cookie Name:"+cookie.name());
                            //System.out.println("cookie Value:"+cookie.value());
                        }
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                        List<Cookie> cookies = cookieStore.get(httpUrl.host());
                        return cookies != null ? cookies : new ArrayList<Cookie>();

                    }
                }).build();

        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("wechat_qr_code1", "wechat_qr_code1.jpg", fileBody)
                .addFormDataPart("id", id)
                .build();
        String url = String.format("%s/%s", FX_URL, "api/user/wechat_qr_code");
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ////Log.i("xxx","1、连接的消息"+response.body().string());
                if(response.isSuccessful()){
                    //Log.i("xxx","2、连接成功获取二维码"+response.body().string());
                }
            }
        });
    }


    /**
     * 上传微信二维码
     */
    public void type2020(String id,File file) {
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    @Override
                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                        for(Cookie cookie:list){
                            if(cookie.name().equals("nzpay.sesssion")){
                                cookieStore.put(httpUrl.host(), list);
                            }
                        }
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                        List<Cookie> cookies = cookieStore.get(httpUrl.host());
                        return cookies != null ? cookies : new ArrayList<Cookie>();

                    }
                }).build();

        RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("headimg1", "headimg1.jpg", fileBody)
                .addFormDataPart("id", id)
                .build();
        String url = String.format("%s/%s", FX_URL, "api/user/headicon");
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ////Log.i("xxx","1、连接的消息"+response.body().string());
                if(response.isSuccessful()){
                    //Log.i("xxx","2、连接成功获取头像"+response.body().string());
                }
            }
        });
    }


    /**
     * 单独遍历订单
     * @param callBack
     */
    public void type2021(String id,String privacy,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("privacy", privacy);
        String url = String.format("%s/%s", FX_URL, "api/user/privacy");
        asyncJsonObjectByURL(url,map,callBack);
    }


    //////////////////////////////////王志

    /**
     * 四要素验证
     * @param user_id
     * @param user_id
     * @param user_id
     * @param cardnumber
     * @param cardhomephone
     * @param callBack
     */
    public void type1001(String user_id,String realname,String idnumber,String cardnumber,String cardhomephone,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("realname", realname);
        map.put("idnumber", idnumber);
        map.put("cardnumber", cardnumber);
        map.put("account", cardhomephone);
        String url = String.format("%s/%s", WZ_URL, "api/user/bankCardverify");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 上传身份证(未实现)
     * @param user_id
     * @param idnumber_zm1
     * @param idnumber_fm1
     * @param callBack
     */
    public void type1002(String user_id,String idnumber_zm1,String idnumber_fm1,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("idnumber_zm1", idnumber_zm1);
        map.put("idnumber_fm1", idnumber_fm1);
        String url = String.format("%s/%s", WZ_URL, "api/user/idnumber");
        sendComplexForm(url,map,callBack);
    }

    /**
     * 人脸识别
     * @param user_id
     * @param idnumber
     * @param realname
     * @param callBack
     */
    public void type1003(String user_id,String idnumber,String realname,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("idnumber", idnumber);
        map.put("realname", realname);
        String url = String.format("%s/%s", WZ_URL, "api/user/certifyFace");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 人脸识别回调验证
     * @param user_id
     * @param biz_no
     * @param callBack
     */
    public void type1004(String user_id,String biz_no,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("biz_no", biz_no);
        String url = String.format("%s/%s", BASE_URL, "api/user/certified");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 查询游戏列表
     * @param callBack
     */
    public void type1005(final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("", "");
        String url = String.format("%s/%s", BASE_URL, "api/game/list");
        asyncJsonObjectByURLFromGET(url,map,callBack);
    }

    /**
     *  根据id查询具体信息
     * @param id
     * @param callBack
     */
    public void type1006(String id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", WZ_URL, "api/user/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 疯豆转疯币
     * @param id
     * @param number
     * @param callBack
     */
    public void type1007(String id,String number,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("number", number);
        String url = String.format("%s/%s", WZ_URL, "api/user/exchangegameicon");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 疯币转疯豆
     * @param id
     * @param number
     * @param callBack
     */
    public void type1008(String id,String number,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("number", number);
        String url = String.format("%s/%s", WZ_URL, "api/user/exchangecredit");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 添加银行卡
     * @param cardnumber
     * @param user_id
     * @param phone
     * @param type
     * @param callBack
     */
    public void type1009(String cardnumber,String user_id,String phone,String type,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("cardnumber", cardnumber);
        map.put("user_id", user_id);
        map.put("phone", phone);
        map.put("type", type);
        String url = String.format("%s/%s", WZ_URL, "api/card/add");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 账单明细
     * @param account
     * @param callBack
     */
    public void type1010(String account,String autoclear,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("autoclear", autoclear);
        String url = String.format("%s/%s", WZ_URL, "api/user/MyBill01");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 通道列表
     * @param levelnumber
     * @param tunnel_code
     * @param callBack
     */
    public void type1011(String levelnumber,String tunnel_code,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("levelnumber", levelnumber);
        map.put("cleartype", tunnel_code);
        String url = String.format("%s/%s", WZ_URL, "/api/leveltunnel/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 获取信用卡列表
     * @param uesr_id
     * @param type
     * @param callBack
     */
    public void type1012(String uesr_id,String type,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", uesr_id);
        map.put("type", type);
        String url = String.format("%s/%s", WZ_URL, "/api/card/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 跳转页面
     * @param uesr_id
     * @param tunnel_code
     * @param txnAmt
     * @param card_id
     * @param callBack
     */
    public void type1013(String uesr_id,String tunnel_code,String txnAmt,String card_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", uesr_id);
        map.put("tunnel_code", tunnel_code);
        map.put("txnAmt", txnAmt);
        map.put("accNo",card_id);
        String url = String.format("%s/%s", WZ_URL, "/api/pay/unionpay/check");
        asyncJsonObjectByURL(url,map,callBack);
    }

    public void type1014(final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        String url = String.format("%s/%s", WZ_URL, "/api/user/selectFL");
        asyncJsonObjectByURL(url,map,callBack);
    }
    public void type1015(String user_id,String money,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id",user_id);
        map.put("money",money);
        String url = String.format("%s/%s", WZ_URL, "/api/pay/checkout");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 收款明细
     * @param account
     * @param callBack
     */
    public void type1016(String account,String BillType,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("account", account);
        map.put("BillType", BillType);
        String url = String.format("%s/%s", WZ_URL, "api/user/MyBill");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 查看我的团队
     * @param user_id
     * @param callBack
     */
    public void type1017(String user_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        String url = String.format("%s/%s", WZ_URL, "api/user/myteam");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 查看我的团队详情
     * @param user_id
     * @param level_id
     * @param certified_status
     * @param callBack
     */
    public void type1018(String user_id,String level_id,String certified_status,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        map.put("level_id",level_id);
        map.put("certified_status",certified_status);
        String url = String.format("%s/%s", WZ_URL, "/api/user/myteamLength");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 地址列表
     * @param user_id
     * @param callBack
     */
    public void type1019(String user_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", user_id);
        String url = String.format("%s/%s", WZ_URL, "/api/address/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    public void type1020(String user_id,String receiver_name,String receiver_phone,String province,String city,String district,String address,String isdefault,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", user_id);
        map.put("receiver_name", receiver_name);
        map.put("receiver_phone", receiver_phone);
        map.put("province", province);
        map.put("city", city);
        map.put("district", district);
        map.put("address", address);
        map.put("isdefault", isdefault);
        String url = String.format("%s/%s", WZ_URL, "/api/address/add");
        asyncJsonObjectByURL(url,map,callBack);
    }
    public void type1021(String id,String user_id,String receiver_name,String receiver_phone,String province,String city,String district,String address,String isdefault,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("user_id", user_id);
        map.put("receiver_name", receiver_name);
        map.put("receiver_phone", receiver_phone);
        map.put("province", province);
        map.put("city", city);
        map.put("district", district);
        map.put("address", address);
        map.put("isdefault", isdefault);
        String url = String.format("%s/%s", WZ_URL, "/api/address/update");
        asyncJsonObjectByURL(url,map,callBack);
    }

    public void type1022(String user_id,String address_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", user_id);
        map.put("address_id",address_id);
        String url = String.format("%s/%s", WZ_URL, "/api/address/isdefault");
        asyncJsonObjectByURL(url,map,callBack);
    }
    public void type1023(String id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        String url = String.format("%s/%s", WZ_URL, "/api/address/del");
        asyncJsonObjectByURL(url,map,callBack);
    }
    public void type1024(String id,String type,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("user_id", id);
        map.put("type", type);
        String url = String.format("%s/%s", WZ_URL, "/api/card/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    public void  type1025(String id,String idnumber_zm1,String idnumber_fm1,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        map.put("idnumber_zm1", idnumber_zm1);
        map.put("idnumber_fm1", idnumber_fm1);
        String url = String.format("%s/%s", WZ_URL, "/api/user/idnumber");
        sendComplexForm(url,map,callBack);
    }

    public void type1026(String parent_user_id,final func4 callBack){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", parent_user_id);
        String url = String.format("%s/%s", WZ_URL, "/api/user/list");
        asyncJsonObjectByURL(url,map,callBack);
    }

    /**
     * 上传微信二维码
     */
    public void type1027(String id,File file1,File file2) {
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    @Override
                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                        for(Cookie cookie:list){
                            if(cookie.name().equals("nzpay.sesssion")){
                                cookieStore.put(httpUrl.host(), list);
                            }
                        }
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                        List<Cookie> cookies = cookieStore.get(httpUrl.host());
                        return cookies != null ? cookies : new ArrayList<Cookie>();

                    }
                }).build();

        RequestBody fileBody1 = RequestBody.create(MediaType.parse("image/png"), file1);
        RequestBody fileBody2 = RequestBody.create(MediaType.parse("image/png"), file2);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("idnumber_zm1", "headimg1.jpg", fileBody1)
                .addFormDataPart("idnumber_fm1", "headimg2.jpg", fileBody2)
                .addFormDataPart("id", id)
                .build();
        String url = String.format("%s/%s", WZ_URL, "api/user/idnumber");
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.e("xxx","失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ////Log.i("xxx","1、连接的消息"+response.body().string());
                if(response.isSuccessful()){
                    //Log.i("xxx","2、连接成功获取头像"+response.body().string());
                }
            }
        });
    }
    /**
     * 上传微信二维码
     */
    public void type1028(String id,File file1) {
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new CookieJar() {
                    @Override
                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                        for(Cookie cookie:list){
                            if(cookie.name().equals("nzpay.sesssion")){
                                cookieStore.put(httpUrl.host(), list);
                            }
                        }
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                        List<Cookie> cookies = cookieStore.get(httpUrl.host());
                        return cookies != null ? cookies : new ArrayList<Cookie>();

                    }
                }).build();

        RequestBody fileBody1 = RequestBody.create(MediaType.parse("image/png"), file1);

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("cardnumber_zm1", "cardnumber_zm1.jpg", fileBody1)
                .addFormDataPart("id", id)
                .build();
        String url = String.format("%s/%s", WZ_URL, "api/user/cardnumber");
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.e("xxx","失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ////Log.i("xxx","1、连接的消息"+response.body().string());
                if(response.isSuccessful()){
                    //Log.i("xxx","2、连接成功获取头像"+response.body().string());
                }
            }
        });
    }
}
